package com.woniuxy.controller;

import com.woniuxy.annotation.RequirePerms;
import com.woniuxy.entity.Goods;
import com.woniuxy.entity.vo.GoodsVO;
import com.woniuxy.service.GoodsService;
import com.woniuxy.utils.CustomPage;
import com.woniuxy.utils.JWTUtil;
import com.woniuxy.utils.ResponseResults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/goods")
//@CrossOrigin    // 允许跨域访问当前controller的所有接口
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * PathVariable 注解
     * GetMapping("/findByCategoryId/{abc}") ---> @PathVariable("abc") int categoryId: 将abc传给categoryId
     * 原来的传值路径：findByCategoryId?id=xxx&name=xxx
     * 新的传值路径： findByCategoryId/xxx/xxx   无法识别哪个参数对应哪个参数，可防职场小人
     * */

    @GetMapping("/findByCategoryId/{categoryId}")
    public ResponseResults<List<GoodsVO>> findByCategoryId(@PathVariable("categoryId") int categoryId){
        System.out.println("findByCategoryId");
        ResponseResults<List<GoodsVO>> responseResults = goodsService.findByCategoryId(categoryId);
        System.out.println(responseResults);
        return responseResults;
    }


    @GetMapping("/getDetailByGid/{gid}")
    public ResponseResults<Goods> getDetailByGid (@PathVariable("gid") int gid){
        return goodsService.getDetailByGid(gid);
    }

    @GetMapping("/delById/{gid}/{categoryId}")
    public ResponseResults<Boolean> delById(@PathVariable("gid") int gid, @PathVariable("categoryId") int categoryId){
        //
        return goodsService.delById(gid,categoryId);
    }

    @GetMapping("/findByAll/{page}/{size}")
    public ResponseResults<CustomPage<List<Goods>>> findByUid(HttpServletRequest request,
                                                              @PathVariable("page") int page,
                                                              @PathVariable("size") int size){
        // 1. 从token中取出uid
        int uid = JWTUtil.getuid(request.getHeader("authorization"));
        // 2. 根据uid查询商品信息, 并返回响应数据
        return goodsService.findByUid(uid,page,size);
    }


    @PreAuthorize("hasAuthority('goods:update')")
    @PutMapping("/update")
    public ResponseResults<Boolean> update(@RequestBody Goods goods){
        return goodsService.update(goods);
    }

//    @RequirePerms("goods:add")
    @PreAuthorize("hasAuthority('abc:aaa')")
    @PostMapping("/add")
    // TODO 将新添加的商品显示在首行  ResponseResults<CustomPage<List<Goods>>>
    public ResponseResults<CustomPage<List<Goods>>> add(@RequestBody Goods goods, HttpServletRequest request){
        System.out.println(goods.toString());
        int getuid = JWTUtil.getuid(request.getHeader("authorization"));
        // 从token中获取uid存入good对象
        goods.setMid(getuid);
        goodsService.add(goods);
        return null;
    }
}
